package com.wanmait.deliverymangementsystem.runnercontroller;

import com.wanmait.deliverymangementsystem.pojo.City;
import com.wanmait.deliverymangementsystem.pojo.Province;
import com.wanmait.deliverymangementsystem.pojo.Runner;
import com.wanmait.deliverymangementsystem.service.CityService;
import com.wanmait.deliverymangementsystem.service.OrdersService;
import com.wanmait.deliverymangementsystem.service.ProvinceService;
import com.wanmait.deliverymangementsystem.service.RunnerService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("runner")
public class RunnerController {

    @Resource
    CityService cityService;
    @Resource
    RunnerService runnerService;
    @Resource
    OrdersService ordersService;

    @Resource
    private ProvinceService provinceService;

    //注册
    @GetMapping("register")
    @ModelAttribute("provinceList")
    public List<Province> register(Model model){
        List<Province> provinceList =provinceService.findAll();
        List<City> beijingList=cityService.findCityByProvinceID(1);
        model.addAttribute("beijingList",beijingList);
        return provinceList;
    }

    @PostMapping("register")
    public String register(Runner runner, MultipartFile runnerFace, HttpServletRequest rquest){

        if (!runnerFace.isEmpty()){
            String filePath= rquest.getServletContext().getRealPath("/static/upimages");
            String oriName= runnerFace.getOriginalFilename();
            String extname = FilenameUtils.getExtension(oriName);
            String file= UUID.randomUUID().toString()+"."+extname;
            runner.setFace(file);
            try {
                runnerFace.transferTo(new File(filePath+"/"+file));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        else
            runner.setFace("1.jpeg");
        runnerService.register(runner);
        return "redirect:/runner/login";
    }

   //登录
    @GetMapping("login")
    public void login(String error,Model model){
        if (error!=null){
            switch (error){
                case "1":
                    error="手机号或者密码错误";
                    break;
                case "2":
                        error="账号已冻结";
                    break;
            }
            model.addAttribute("error", error);
        }
    }
    @PostMapping("login")
    public String login(Runner runner, HttpSession session){
      Runner loginrunner =  runnerService.Login(runner);

      if (loginrunner==null){

          return  "redirect:/runner/login?error=1";
      }
      else if(!loginrunner.getEnable())
      {
          return  "redirect:/runner/login?error=2";
      }

      else {
          session.setAttribute("runner",loginrunner);
            if (ordersService.findRunnerTaskingNumber(loginrunner.getId())<5){
                runnerService.taskAvailable(true,loginrunner.getId());
            }
          return "redirect:/runner/index";
      }
    }

    //钱包页面
    @GetMapping("wallet")
    public void wallet(){

    }

    @GetMapping("wallets")
    public String wallets(@SessionAttribute Runner runner,HttpSession session){
        runner.setWallet(0.0);
        runnerService.updateWallet(runner);
        session.setAttribute("runner",runner);
        return "redirect:/runner/wallet";
    }
}
